﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using System.Data.SqlClient;
using System.Data;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using bdxyit.com.RecruitmentAppSys.Models;

/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：EthnicList
*系统时间：3/25/2012 3:35:05 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:35:05 PM
*修改事件：
***/
namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class EthnicListDAL : IEthnicListDAL
    {
        private const string SQL_SELECT_ETHNICLIST = " select   EthnicID,EthnicName from EthnicLists ";
        private const string SQL_SELECT_ETHNICLIST_BY_ETHNICID = " select  top 1 EthnicID,EthnicName from EthnicLists "
                                                                + "  where EthnicID=@EthnicID";
        private const string SQL_DELETE_ETHNICLIST_BY_ETHNICID = " delete from EthnicLists "
                                                                 + "  where EthnicID=@EthnicID";
        private const string SQL_UPDATE_ETHNICLIST_BY_ETHNICID = " update EthnicLists set "
                                                                + " EthnicID=@EthnicID,"
                                                                + " EthnicName=@EthnicName"
                                                                + "  where EthnicID=@EthnicID";
        private const string SQL_INSERT_ETHNICLIST = " insert into EthnicLists("
                                                    + " EthnicID,EthnicName)"
                                                    + "  values ("
                                                    + " @EthnicID,@EthnicName)";
        private const string FIELD_EthnicID = "@EthnicID";
        private const string FIELD_EthnicName = "@EthnicName";
        public bool InsertEthnicList(EthnicList cmodel)
        {
            bool flag = false;
            SqlParameter[] EthnicListParms = GetEthnicListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            EthnicListParms[0].Value = cmodel.EthnicID;
            EthnicListParms[1].Value = cmodel.EthnicName;

            foreach (SqlParameter parm in EthnicListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_ETHNICLIST;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteEthnicList(string EthnicID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_EthnicID, SqlDbType.VarChar, 10);
            parm.Value = EthnicID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_ETHNICLIST_BY_ETHNICID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateEthnicList(EthnicList cmodel)
        {
            bool flag = false;
            SqlParameter[] EthnicListParms = GetEthnicListParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句

            EthnicListParms[0].Value = cmodel.EthnicID;
            EthnicListParms[1].Value = cmodel.EthnicName;
            foreach (SqlParameter parm in EthnicListParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_ETHNICLIST_BY_ETHNICID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public EthnicList SelectEthnicListByEthnicID(string EthnicID)
        {
            EthnicList cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_EthnicID, SqlDbType.VarChar, 10);
            parm.Value = EthnicID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_ETHNICLIST_BY_ETHNICID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new EthnicList();
                    if (dr.HasRows == true)
                    {
                        if (dr["EthnicID"] != null && dr["EthnicID"].ToString() != "")
                        {
                            cmodel.EthnicID = dr["EthnicID"].ToString();
                        }
                        if (dr["EthnicName"] != null && dr["EthnicName"].ToString() != "")
                        {
                            cmodel.EthnicName = dr["EthnicName"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<EthnicList> SelectEthnicList()
        {
            List<EthnicList> selectlist = new List<EthnicList>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_ETHNICLIST))
            {
                while (dr.Read())
                {
                    EthnicList cmodel = new EthnicList();
                    //写添赋值语句
                    if (dr["EthnicID"] != null && dr["EthnicID"].ToString() != "")
                    {
                        cmodel.EthnicID = dr["EthnicID"].ToString();
                    }
                    if (dr["EthnicName"] != null && dr["EthnicName"].ToString() != "")
                    {
                        cmodel.EthnicName = dr["EthnicName"].ToString();
                    }

                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetEthnicListParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter(FIELD_EthnicID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_EthnicName, SqlDbType.VarChar,50)};
            return parms;
        }


    }
}